package com.oopnet.template.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;

import com.oopnet.template.test.TranslateLanguage;
import com.sanmina.mes.storage.StorageClient;
import com.sanmina.mes.storage.factory.StorageClientFactory;

//@WebServlet("/translate")
@RestController
public class TranslateController {
	// 方法间类实例变量共享
	private int i = 1;

	/*
	 * @Override protected void doGet(HttpServletRequest req,
	 * HttpServletResponse resp) throws ServletException, IOException {
	 * 
	 * // return s; }
	 * 
	 * 
	 * @Override protected void doPost(HttpServletRequest req,
	 * HttpServletResponse resp) throws ServletException, IOException {
	 * this.doGet(req, resp); }
	 */
	@RequestMapping(value = "translate", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public void test() {

	}

	@RequestMapping(value="translate?",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public Map<String, Object> translate(ModelAndView modelAndView,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession) throws IOException{
//		HttpSession session = request.getSession();
		System.out.println(httpSession.getId());
		httpSession.setMaxInactiveInterval(20);
		this.i++;
		System.out.println(i);
		System.out.println(modelAndView.getModel());
		//		session.setMaxInactiveInterval(20);
//		StorageClient client = StorageClientFactory
//				.buildStorageClient(StorageClient.GOOGLE_STORAGE);
//		
//		System.out.println(client.listAllOfBucket("sanm-ml"));
//		
//		StorageClient client2 = StorageClientFactory
//				.buildStorageClient(StorageClient.AMAZON_STORAGE);
//		File file = new File(TranslateController.class.getResource("/").getPath()+"/google-storage.json");
//		System.out.println(client2.upload("cirruslog2", "/mesdoc/documents/workstation/2017/02/27/", file));
//		System.out.println(client2.list("cirruslog2", "/mesdoc/documents/workstation/20"));
//		System.out.println("**********************************************");
//		System.out.println(file.exists());
//		System.out.println("**********************************************");
		TranslateLanguage tl = TranslateLanguage.getInstance();
		response.setContentType("application/json;charset=UTF-8");
		String s = tl.translate("Synchronize Agile", "zh_CN");
		Cookie[] cookies = request.getCookies();
		System.out.println(cookies);
//		PrintWriter out = response.getWriter();
		if (cookies!=null) {
			for (Cookie cookie : cookies) {
				System.out.println(cookie.getName()+": "+cookie.getValue());
				if (cookie.getName().equals("LastAccessTime")) {
					Long lastAccessTime =Long.parseLong(cookie.getValue());
					Date date = new Date(lastAccessTime);
//					out.write(date.toLocaleString());
				}
			}
		}
		System.out.println(s);
		Cookie selfCookie = new Cookie("LastAccessTime", System.currentTimeMillis()+"");
		response.addCookie(selfCookie);
//		out.write(s);
		 Map<String, Object> map = new HashMap<String,Object>();
		 map.put("Str1", new String("Value"));
		 return map;
//		return s;
	}

}
